var timeX=[];
$('#tt').edatagrid({
	   onBeforeSave:function(index){
		   var oldTimeRow= parseInt($('#tt').datagrid('getData').rows[index].timePercent);
		   var oldTImeVal=parseFloat($("td[field='timePercent'] input").val());
		   var prevRow='',nextRow='';
		   var prevTimePercent=0;
		   var nextTimePercent=100;
		   var len=$('#tt').datagrid('getData').rows.length-1;
		   if(index!=0){
			   prevRow=$('#tt').datagrid('getData').rows[index-1];
			   prevTimePercent=parseFloat(prevRow.timePercent);
		   }
		   if(index!=len){
			   nextRow=$('#tt').datagrid('getData').rows[index+1];
			   nextTimePercent=parseFloat(nextRow.timePercent);
		   }
		   if(oldTImeVal<prevTimePercent){
			   oldTImeVal=oldTimeRow;
			   jQuery.messager.alert('提示:','占比应大于前一时刻!');
			   return false;
		   }
		   if(oldTImeVal>nextTimePercent){
			   oldTImeVal=oldTimeRow;
			   jQuery.messager.alert('提示:','占比应小于后一时刻!');
			   return false;
		   }
		   if($("td[field='timePercent'] input").val()==''){
			   jQuery.messager.alert('提示:','占比不能为空!');
			   return false;
		   }
	   }
	   
	});
var getTimeX=function(){
	var str='',k='';
    for(var i=0,m=0;m<24;i++){
        if(i==60){
            m++;
            i=00;
        }
        if(i%15==0){
          if(i==0){
            k='0';
          }else{
            k='';
          }
          if(m<10){
            str='0'+m+':'+i+k+':00';
          }else{
            str=m+':'+i+k+':00';
          }
           timeX.push(str)  
        }   
    }
    return timeX;
}
function doSearch(){    
	$('#templeteName').combobox("clear");
    $('#tt').datagrid('load', {    
    	mark: $('#mark').val(),
    	channelName: $('#channelName').val(),
    	apkName: $('#apkName').val()
    });
    if( $('#mark').val()!=''){
    	$("#container").css({'display':'block'});
        var timeData=[];
        var getArray=[];
        var getArray2=[];
        var  markVal=$('#mark').val();
        getTimeX();
        if(markVal!=''){
        		$.ajax({
        			url:"/channelLimitModel/retentmodel?json",
        			data:{'mark':markVal},
        			async:false,
        			success:function(data){
        	        	data=data.rows;
        				if(data.length!=0){
        					$("#container").css({'width':'100%','height':'600px','margin':'0 auto'});
        				}else{
        					$("#container").css({'display':'none'});
        				}
        	        	$.each(data,function(index,mod){
        	        		getArray.push(mod.timePercent);
        	        		getArray2.push(mod.spDayLimit)
        	        	});
        			}
        		})
        }
        var chart = new Highcharts.Chart({
            chart: {
                renderTo: 'container',
                animation: false
            },
            
            title: {
                text: '数据统计图' //标题
            },
            xAxis: {
                categories:timeX //x轴
            },
            yAxis:{
            	max: 101,
            },
            plotOptions: {
                series: {
                    point: {
                        events: {
                            drag: function (e) {
                               // Returning false stops the drag and drops.
                                var prePointY,nextPointY;
                                var f=parseInt(e.x)+1;//获取后一个点的
                                var oldY=chart.series[0].data[f];
                                if(this.x==0){
                                    prePointY=0;
                                }else{
                                    prePointY = this.series.points[this.x-1].y;
                                }
                                if(oldY){
                                    nextPointY = this.series.points[this.x+1].y;
                                    if(e.y > nextPointY){
                                    this.y = nextPointY;
                                    e.y=nextPointY;
                                    this.events.drop(e);
                                    return false;
                                    }
                                }
                                if(e.y <= prePointY){
                                    this.y = prePointY;
                                    e.y=prePointY;
                                    this.events.drop(e);                                   
                                    return false;
                                }
                                $('#drag').html(
                                    'Dragging <b>' + this.series.name + '</b>, <b>' + this.category + '</b> to <b>' + Highcharts.numberFormat(e.y, 2) + '</b>');
                            },
                            drop:function (e) {
                                $('#drop').html(
                                    'In <b>'+e.target.category+'</b> was set to <b>' + e.y + '</b>');
                                var time=e.target.category;
                                var timePercent=e.y;
        						var spDayLimit=getArray2[e.x];
                                $.ajax({
                                	url:'/channelLimitModel/retentmodelUpdate',
                                	type:'post',
                                	data:{
                                		'mark':markVal,
                                		'time':time ,
                                		'timePercent':timePercent,
                                		'spDayLimit':spDayLimit,
                                		'type':1
                                		},
                                	success:function(){
                                		doSearch();
                                	}
                                });
                            }
                        }
                    },
                    stickyTracking: false
                },
                column: {
                    stacking: 'normal'
                },
                line: {
                    cursor: 'ns-resize'
                }
            },
            tooltip: {
                yDecimals: 2
            },
            series: [{
            	data:getArray,
                draggableY: true,
                dragMinY: 0,
                dragMaxY:100
            }]
        });
        $(".highcharts-credits").hide();
    }else{
    	$("#container").css({'display':'none'});
    }
}
function add(){
    showMyWindow('模拟配置管理 |模拟新增配置管理', '/channelapk/addConfig', 800, 500);
}

function edit(){
	var row = $('#tt').datagrid('getSelected');
	var mark = row.mark;
    if (row){
	    showMyWindow('模拟器配置管理 | 编辑模拟器配置', '/channelapk/'+mark+'/configUpdate', 700, 500);
    } else {
    	showMsg(market.content.title, market.content.tips, alert);
    }
}

function del(){
	var row = $('#tt').datagrid('getSelected');
	var mark = row.mark;
	var provinceid = row.provinceid;
    $.messager.confirm('温馨提醒','你确定删除本条数据吗？',function(r){
        if(r){
            $.get('/channelapk/provinceDelete?mark='+mark+'&provinceid='+provinceid,function(data){
                if (data.return_code == '1') {
					top.showMsg(market.content.title, data.return_msg, alert);
					doSearch();
				}else if(data.return_code == '3'){
					top.showMsg(market.content.title, data.return_msg, alert);
				} else {
					$.messager.alert(market.content.title, data.return_msg);
				}
            });
        }
    });
}

function formatterModel(value,row,index){
	 var a = '<a href="#" onclick="editModel('+"'"+row.mark+"'"+')">配置</a> ';
	 return a;
}


function formatterValue(value,row,index){
	 return value.toFixed(2);;
}
function saveRow(){
	$('#tt').edatagrid('saveRow');
	doSearch();
}

$(function(){
	 $('#tt').edatagrid({
       updateUrl: '/channelLimitModel/retentmodelUpdate',
       url: '/channelLimitModel/retentmodel?json'
    });
	//输入框数字限制，最大为100且只最多能有两位小数点 
	$(document).on("keyup afterpaste","td[field='timePercent'] input",function(e){
		 this.value = this.value.replace(/[^\d.]/g,"");  //清除“数字”和“.”以外的字符   
		  this.value = this.value.replace(/\.{2,}/g,"."); //只保留第一个. 清除多余的   
		  this.value = this.value.replace(".","$#$").replace(/\./g,"").replace("$#$",".");  
		  this.value = this.value.replace(/^(\-)*(\d+)\.(\d\d).*$/,'$1$2.$3');//只能输入两个小数   
		  if(this.value.indexOf(".")< 0 && this.value !=""){//以上已经过滤，此处控制的是如果没有小数点，首位不能为类似于 01、02的金额  
		   this.value= parseFloat(this.value);  
		  }  
		 if(this.value>100){
			 this.value = 100;
		 }
	 })

});	